package com.androidhive.androidlistview;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

public class BidPage extends Activity {
	SessionManager session;
	private WebView webView1,webView2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    Intent intent = getIntent();
	    int cropId  = Integer.parseInt(intent.getStringExtra(MyWebviewFarmer.cropId));
       // String CropId = user.get(SessionManager.Crop_Id);
      //  Log.d("InFarmerCropsfarmer:",farmerId);
	   // setContentView(R.layout.main);
        ScrollView sv = new ScrollView(this);
        DatabaseHandler db = new DatabaseHandler(this);
        TableLayout ll=new TableLayout(this);
        ll.setBackgroundColor(108);
       this.setTitle("Bids Details");
        HorizontalScrollView hsv = new HorizontalScrollView(this);
        LayoutParams layoutParams = new LayoutParams(
        	    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        	layoutParams.setMargins(20, 22, 20, 20);

        	 ll.setLayoutParams(layoutParams); 
        int flag=1;
    TextView tv=new TextView(this);
  	
    		    String responseforcrop = null;
            	try{
    		 //   responseforcrop = CustomHttpClient.executeHttpPost("http://192.16.12.131/crops.php",postParameters);
            	  //  String rk=responseforcrop.toString();
            		String rk=db.getBidOnCrops(cropId);
            		if(rk.contains("#"))
            				{
//            		  HashMap<String, String> user = session.getUserDetails();
//            	        
//            	        // name
//            	       
//            	        
//            	        // email
//            	        String VendorId = (user.get(SessionManager.Vendor_Id)); 
//            	        String CropId = user.get(SessionManager.Crop_Id);
//            	        Log.d("Vendor:",VendorId);
//            	        Log.d("Crop:",CropId );
            		
            	    String [] values=new String[]{};
            	    Log.d("InBidPage:","ab1");
            	   // String [] header=new String[]{};
            	   
            	   // String [] header=new String[]{"No.","CropName","Quality","BaseCost/ton(Rs.)","Current Bid Price","Farmer"};
            	    String[] rows=rk.split("</br>");
            		//System.out.println(rows.length);
            		for(int i=0;i<rows.length;i++){
            			
            		
            			if(flag==1){
            				TableRow tbrow1=new TableRow(this);
            				for(int j=0;j<4;j++){
                				
                				TextView tv1=new TextView(this);
                				String str=new String();
                				 if(j==0)
                		    		 str="vendor Name"; 
                		    	 else if(j==1)
                		    		 str="Bid Price(Rs.)"; 
                				 //	 else if(country.getName().equalsIgnoreCase("Crop Quantity"))
                	    	//	 str="quantity"; 
                		    	 else if(j==2)
                		    		 str="Quantity (In Ton)"; 
                		    	 else if(j==3)
                		    		 str="Package Cost"; 
                		    	
                				tv1.setHeight(100);
                				tv1.setWidth(100);
                				 tv1.setText(str);
                				 tv1.setTextSize(18);
                				 tv1.setTypeface(null,Typeface.BOLD);
                				 tv1.setPadding(5, 5, 5, 5);
                	             tbrow1.addView(tv1);
                	             flag=0;
                				//System.out.print(values[j]+"  ");
                			
                			}
                			
            				ll.addView(tbrow1);
            				
            				
            				
            			}
            			   Log.d("InBidPage:","ab2");
            			TableRow tbrow1=new TableRow(this);
            			///System.out.println();
            			//System.out.println(rows[i]+"\n");
            			values=rows[i].split("#");
            			for(int j=values.length-1;j>0;j--){
            				
            			if(j==values.length-1){
            				webView2= new WebView(this);
            				webView2.setBackgroundColor(Color.BLACK);
            				//webView1.
            				String summary = "<html><head><title>Title of the document</title></head><body><h4><a href=hk"+values[0].toString()+">"+values[j].toString()+"</a></h4></body></html>";
            			       // String summary = "<html><head><title>Title of the document</title></head><body><h4><a href=\"hrupin://second_activity\">Start Bid</a></h4></body></html>";
            			        webView2.loadData(summary, "text/html", null);
            			        webView2.setWebViewClient(new MyWebViewvendorProfile(this));
            			        tbrow1.addView(webView2);
            				
            			}else{
            				TextView tv1=new TextView(this);
            				tv1.setHeight(50);
            				tv1.setWidth(100);
            				 tv1.setText(values[j]);
            				 tv1.setTextSize(15);
            				 tv1.setPadding(5, 5, 5, 5);
            	             tbrow1.addView(tv1);
            				//System.out.print(values[j]+"  ");
            				}
            			
            			}
            			ll.addView(tbrow1);
            		}
            	    
            		   Log.d("InBidPage:","ab3");
            	    
            		hsv.addView(ll);
                    sv.addView(hsv);
                    setContentView(sv); 
            	    
            	    
            				}  	 
            		else{
            			tv.setText("No Bids Available.");
            			 tv.setTypeface(null,Typeface.BOLD);
            			 tv.setTextSize(18);
               		 setContentView(tv);
            		}
            		
    	    	
    	   
    	  
    	} catch (Exception e) {
    		tv.setText(e.toString());
    		 setContentView(tv);
    		   Log.d("InBidPage:","ab4");
    	}
            	
	    // Create the text view
    	
	  
    	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_display_message, menu);
		return true;
	}

	//@Override
	

}
